﻿Imports System.IO
Imports System.Text
Imports System.Threading

Public Class MainForm
    Public textdoc As BlueRichText
    Private read, saved As String
    Private old_pos As Integer = 0
    Private updock As Integer = 78

#Region "MainForm Methods"

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        textdoc = New BlueRichText(txtMain)
        Me.Size = My.Settings.Size
        read = My.Resources.readInfo
        saved = My.Resources.saveInfo
        Prepare_Form()
    End Sub

    Private Sub Prepare_Form()
        Dim language As String
        statInfo.Text = ""
        statEncoding.Text = textdoc.TextEncoding.EncodingName
        statAbout.Text = "v." & Me.ProductVersion & statAbout.Text
        language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName
        If language = "de" Then
            conLanguageEng.Checked = False
            conLanguageDe.Checked = True
        Else
            conLanguageEng.Checked = True
            conLanguageDe.Checked = False
        End If
        Dim allfonts As New Drawing.Text.InstalledFontCollection
        For Each fonttype In allfonts.Families
            comFont.Items.Add(fonttype.Name)
        Next
        comFont.SelectedItem = "Lucida Console"
        comSize.SelectedItem = "10"
        conToolbar.Checked = Not My.Settings.Toolbar
        conToolbar_Click(Nothing, Nothing)
    End Sub

    Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        txtMain.Focus()
        If My.Application.CommandLineArgs.Count > 0 Then
            Dim starting_arg As String = My.Application.CommandLineArgs(0)
            If starting_arg = "/print" Then
                txtMain.Text = File.ReadAllText(My.Application.CommandLineArgs(1))
            ElseIf File.Exists(starting_arg) Then
                textdoc.LoadFileFrom(starting_arg)
                setTitles()
            End If
        End If
    End Sub

    Private Sub MainForm_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
        txtMain.Width = Me.ClientSize.Width
        txtMain.Height = Me.ClientSize.Height - updock
    End Sub

#End Region

#Region "Textbox Events"

    Private Sub txtMain_TextChanged(sender As Object, e As EventArgs) Handles txtMain.TextChanged
        If txtMain.Rtf = textdoc.Text Then
            statInfo.Text = saved
        Else
            statInfo.Text = ""
        End If
        If txtMain.CanUndo Then butUndo.Enabled = True Else butUndo.Enabled = False
        If txtMain.CanRedo Then butRedo.Enabled = True Else butRedo.Enabled = False
    End Sub

    Private Sub txtMain_KeyDown(sender As Object, e As KeyEventArgs) Handles txtMain.KeyDown
        If e.KeyCode = Keys.F1 Then
            conAbout_Click(sender, e)
        End If
    End Sub

    Private Sub txtMain_CursorChanged(sender As Object, e As EventArgs) Handles txtMain.SelectionChanged
        Dim pos As Integer = txtMain.SelectionStart
        If old_pos = pos Then
            pos = pos + txtMain.SelectionLength
        End If
        If old_pos <> txtMain.SelectionStart + txtMain.SelectionLength Then
            old_pos = txtMain.SelectionStart
        End If
        Dim line As Integer = txtMain.GetLineFromCharIndex(pos)
        statPosition.Text = "Ln: " & line + 1
        statPosition.Text &= " | Col: " & pos - txtMain.GetFirstCharIndexFromLine(line) + 1
        statAbout.Text = "v." & Me.ProductVersion & " by Bluegrams 2016"
        set_Format()
    End Sub

#End Region

#Region "Filemenu"

    Private Sub butOpen_Click(sender As Object, e As EventArgs) Handles butOpen.Click, conOpen.Click
        If textdoc.LoadFile() Then
            setTitles()
        End If
        txtMain.Focus()
    End Sub

    Private Sub setTitles()
        Dim file_info As New FileInfo(textdoc.TextPath)
        If file_info.IsReadOnly = True Then
            butSave.Enabled = False
            conSave.Enabled = False
            statInfo.Text = read
        Else
            butSave.Enabled = True
            conSave.Enabled = True
            statInfo.Text = saved
        End If
        Me.Text = "miniEditor - " & file_info.Name
        statEncoding.Text = textdoc.TextEncoding.EncodingName
    End Sub

    Private Sub conPrint_Click(sender As Object, e As EventArgs) Handles conPrint.Click
        textdoc.PrintText()
    End Sub

    Private Sub butSave_Click(sender As Object, e As EventArgs) Handles butSave.Click, conSave.Click
        If textdoc.SaveText() Then statInfo.Text = saved
        txtMain.Focus()
    End Sub

    Private Sub butSaveAs_Click(sender As Object, e As EventArgs) Handles butSaveAs.Click, conSaveAs.Click
        If textdoc.SaveTextAs() Then
            Dim file_info As New FileInfo(textdoc.TextPath)
            Me.Text = "miniEditor - " & file_info.Name
            statInfo.Text = saved
            statEncoding.Text = textdoc.TextEncoding.EncodingName
        End If
        txtMain.Focus()
    End Sub

    Private Sub butNew_Click(sender As Object, e As EventArgs) Handles butNew.Click, conNew.Click
        closing_Dialog(sender, e)
        txtMain.Focus()
    End Sub

    Private Sub conExit_Click(sender As Object, e As EventArgs) Handles conExit.Click
        Me.Close()
    End Sub

#End Region

#Region "Clear/ Close"

    Private Sub MainForm_Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If closing_Dialog(sender, e) = False Then e.Cancel = True
        My.Settings.Size = Me.Size
        My.Settings.Toolbar = conToolbar.Checked
    End Sub

    Private Function closing_Dialog(sender As Object, e As EventArgs) As Boolean
        Dim dosave As DialogResult
        If statInfo.Text <> "" Then
            textclear()
        ElseIf txtMain.Text <> "" Then
            dosave = MessageBox.Show(My.Resources.closeInfo, "miniEditor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
            If dosave = DialogResult.Yes Then
                butSaveAs_Click(sender, e)
                Return False
            ElseIf dosave = DialogResult.No Then
                textclear()
            ElseIf dosave = DialogResult.Cancel Then
                Return False
            End If
        End If
        Return True
    End Function

    Private Sub textclear()
        textdoc.Clear()
        statInfo.Text = ""
        statEncoding.Text = textdoc.TextEncoding.EncodingName
        statPosition.Text = "Ln: 1 | Col: 1"
        Me.Text = "miniEditor"
        butSave.Enabled = False
        conSave.Enabled = False
    End Sub

#End Region

#Region "Editmenu"

    Private Sub contxtEdit_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles contxtEdit.Opening
        If txtMain.SelectionLength > 0 Then
            conCut.Enabled = True
            conCopy.Enabled = True
            conDelete.Enabled = True
        Else
            conCut.Enabled = False
            conCopy.Enabled = False
            conDelete.Enabled = False
        End If
        If butUndo.Enabled = True Then conUndo.Enabled = True Else conUndo.Enabled = False
        If butRedo.Enabled = True Then conRedo.Enabled = True Else conRedo.Enabled = False
    End Sub

    Private Sub conUndo_Click(sender As Object, e As EventArgs) Handles butUndo.Click, conUndo.Click
        txtMain.Undo()
        txtMain.Focus()
    End Sub

    Private Sub butRedo_Click(sender As Object, e As EventArgs) Handles butRedo.Click, conRedo.Click
        txtMain.Redo()
        txtMain.Focus()
    End Sub

    Private Sub conCut_Click(sender As Object, e As EventArgs) Handles conCut.Click
        txtMain.Cut()
    End Sub

    Private Sub conCopy_Click(sender As Object, e As EventArgs) Handles conCopy.Click
        txtMain.Copy()
    End Sub

    Private Sub conPaste_Click(sender As Object, e As EventArgs) Handles conPaste.Click
            txtMain.Paste()
    End Sub

    Private Sub conDelete_Click(sender As Object, e As EventArgs) Handles conDelete.Click
        Dim cursposition As Integer = txtMain.SelectionStart
        txtMain.Text = txtMain.Text.Remove(cursposition, txtMain.SelectionLength)
        txtMain.SelectionStart = cursposition
    End Sub

    Private Sub conSelect_Click(sender As Object, e As EventArgs) Handles conSelect.Click
        txtMain.SelectionStart = 0
        txtMain.SelectionLength = txtMain.Text.Length
    End Sub

#End Region

#Region "Set Language"

    Private Sub conLanguageEng_Click(sender As Object, e As EventArgs) Handles conLanguageEng.Click
        Dim new_culture As New Globalization.CultureInfo("en")
        change_Culture(new_culture, sender, e)
    End Sub

    Private Sub conLanguageDe_Click(sender As Object, e As EventArgs) Handles conLanguageDe.Click
        Dim new_culture As New Globalization.CultureInfo("de")
        change_Culture(new_culture, sender, e)
    End Sub

    Private Sub change_Culture(culture As Globalization.CultureInfo, sender As Object, e As EventArgs)
        Dim old_text As String = txtMain.Rtf
        Dim old_title As String = Me.Text
        Dim old_save As Boolean = butSave.Enabled
        Dim old_richtext As BlueRichText = textdoc
        Thread.CurrentThread.CurrentUICulture = culture
        Thread.CurrentThread.CurrentCulture = culture
        My.Settings.Toolbar = conToolbar.Checked
        My.Settings.Size = Me.Size
        Me.Controls.Clear()
        Me.DataBindings.Clear()
        InitializeComponent()
        MainForm_Load(sender, e)
        txtMain.Focus()
        textdoc = New BlueRichText(txtMain, old_text, old_richtext.TextPath, old_richtext.TextEncoding)
        txtMain.Rtf = old_text
        Me.Text = old_title
        butSave.Enabled = old_save
        statEncoding.Text = textdoc.TextEncoding.EncodingName
    End Sub

#End Region

#Region "Options"

    Private Sub contxtOptions_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles contxtOptions.Opening
        If Path.GetExtension(textdoc.TextPath) = ".rtf" Then
            conEncoding.Enabled = False
        Else
            conEncoding.Enabled = True
        End If
    End Sub

    Private Sub conAbout_Click(sender As Object, e As EventArgs) Handles conAbout.Click
        If Info_Window.Visible = False Then Info_Window.ShowDialog()
    End Sub

    Private Sub conToolbar_Click(sender As Object, e As EventArgs) Handles conToolbar.Click
        If conToolbar.Checked Then
            updock = 45
            toolmain.Visible = False
        Else
            updock = 78
            toolmain.Visible = True
        End If
        MainForm_Resize(sender, e)
        conToolbar.Checked = Not conToolbar.Checked
    End Sub

    Private Sub conEncoding_Click(sender As Object, e As EventArgs) Handles conEncoding.Click
        If statInfo.Text <> "" Then
            Dim Convert_Encoding As New Convert_Encoding()
            Convert_Encoding.ShowDialog()
        Else
            Dim Convert_Encoding As New Convert_Encoding()
            Convert_Encoding.ShowDialog()
        End If
    End Sub

#End Region

#Region "Text Formatting"

    Private Sub set_Format()
        If txtMain.SelectionFont IsNot Nothing Then
            comFont.SelectedItem = txtMain.SelectionFont.Name
            comSize.Text = CStr(CInt(txtMain.SelectionFont.Size))
            butBold.Checked = txtMain.SelectionFont.Bold
            butItalic.Checked = txtMain.SelectionFont.Italic
            butUnderlined.Checked = txtMain.SelectionFont.Underline
            butColour.ForeColor = txtMain.SelectionColor
        Else
            comFont.Text = ""
            comSize.Text = ""
        End If
    End Sub

    Private Sub comFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comFont.SelectedIndexChanged
        If txtMain.SelectionFont IsNot Nothing Then
            txtMain.SelectionFont = New Font(comFont.SelectedItem.ToString, txtMain.SelectionFont.Size, txtMain.SelectionFont.Style)
        Else
            statAbout.Text = My.Resources.notvalid
        End If
        txtMain.Focus()
    End Sub

    Private Sub comSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comSize.SelectedIndexChanged
        If txtMain.SelectionFont IsNot Nothing Then
            txtMain.SelectionFont = New Font(txtMain.SelectionFont.FontFamily.Name, comSize.SelectedItem, txtMain.SelectionFont.Style)
        Else
            statAbout.Text = My.Resources.notvalid
        End If
        txtMain.Focus()
    End Sub

    Private Sub butBold_Click(sender As Object, e As EventArgs) Handles butBold.Click
        butBold.Checked = Not butBold.Checked
        If txtMain.SelectionFont IsNot Nothing Then
            Dim sel As Font = txtMain.SelectionFont
            If sel.Bold Then
                txtMain.SelectionFont = New Font(sel.FontFamily.Name, sel.Size, sel.Style And Not FontStyle.Bold)
            Else
                txtMain.SelectionFont = New Font(sel.FontFamily.Name, sel.Size, sel.Style Or FontStyle.Bold)
            End If
        Else
            statAbout.Text = My.Resources.notvalid
        End If
    End Sub

    Private Sub butItalic_Click(sender As Object, e As EventArgs) Handles butItalic.Click
        butItalic.Checked = Not butItalic.Checked
        If txtMain.SelectionFont IsNot Nothing Then
            Dim sel As Font = txtMain.SelectionFont
            If sel.Italic Then
                txtMain.SelectionFont = New Font(sel.FontFamily.Name, sel.Size, sel.Style And Not FontStyle.Italic)
            Else
                txtMain.SelectionFont = New Font(sel.FontFamily.Name, sel.Size, sel.Style Or FontStyle.Italic)
            End If
        Else
            statAbout.Text = My.Resources.notvalid
        End If
    End Sub

    Private Sub butUnderlined_Click(sender As Object, e As EventArgs) Handles butUnderlined.Click
        butUnderlined.Checked = Not butUnderlined.Checked
        If txtMain.SelectionFont IsNot Nothing Then
            Dim sel As Font = txtMain.SelectionFont
            If sel.Underline Then
                txtMain.SelectionFont = New Font(sel.FontFamily.Name, sel.Size, sel.Style And Not FontStyle.Underline)
            Else
                txtMain.SelectionFont = New Font(sel.FontFamily.Name, sel.Size, sel.Style Or FontStyle.Underline)
            End If
        Else
            statAbout.Text = My.Resources.notvalid
        End If
    End Sub

    Private Sub butColour_Click(sender As Object, e As EventArgs) Handles butColour.Click
        If txtMain.SelectionFont IsNot Nothing Then
            If colDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                butColour.ForeColor = colDialog.Color
                txtMain.SelectionColor = colDialog.Color
            End If
        Else
            statAbout.Text = My.Resources.notvalid
        End If
    End Sub

#End Region

End Class
